/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socof.wget;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 *
 * @author Carlos
 */
public class SOCOFWget
{

    protected static String localPath;
    protected static String site;
    protected static int maxBandwith;
    protected static float maxCpu;
    protected static int downloadersLimit = -1;
    protected static int parsersLimit = -1;
    protected static int maxLevel;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, InterruptedException
    {
        if (!getCommanLineArgs(args))
        {
            System.out.println("Invalid arguments. Valid options are:");
            System.out.println("");
            System.out.println("Mandatory arguments:");
            System.out.println("-localPath=<path to an existing local dir.>");
            System.out.println("-site=<url of site to be downloaded>");
            System.out.println("-maxBandwith=<max. bandwith allowed>");
            System.out.println("-maxCpu=<max. cpu usage (between 0 to 1)>");
            System.out.println("");
            System.out.println("Optional arguments:");
            System.out.println("-downloadersLimit=<max. number of parallel downloades>");
            System.out.println("-parsersLimit=<max. number of parallel parsers>");
            System.out.println("-maxLevel=<max. site level depth>");

            return;
        }

        // base url to db
        WebResource.setBasePath(localPath);
        WebResource wr = new WebResource(site);        
        if (maxLevel > 0)
            WebResource.setMaxLevel(maxLevel); 
        
        PendingQueue.getInstance().offer(wr);

        Laucher lb = Laucher.getInstance();
        lb.setMaxBandwithAllocation(maxBandwith);
        lb.setMaxCpuUsage(maxCpu);
        
        if (downloadersLimit > 0)
            lb.setDownloadersLimit(downloadersLimit);
        
        if(parsersLimit > 0)
            lb.setParsersLimit(parsersLimit);

        lb.start();
        lb.join();
    }

    protected static boolean getCommanLineArgs(String[] args)
    {
        for (String s : args)
        {
            if (s.startsWith("-localPath="))
            {
                localPath = getValFromArg(s);
            }
            if (s.startsWith("-site="))
            {
                site = getValFromArg(s);
            }
            if (s.startsWith("-maxBandwith="))
            {
                try
                {
                    maxBandwith = Integer.parseInt(getValFromArg(s));
                }
                catch (NumberFormatException ex)
                {
                    maxBandwith = 0;
                } 
            }
            if (s.startsWith("-maxCpu="))
            {
                try
                {
                    maxCpu = Float.parseFloat(getValFromArg(s));
                }
                catch (NumberFormatException ex)
                {
                    maxCpu = 0.0f;
                }                
            }
            if (s.startsWith("-downloadersLimit="))
            {
                try
                {
                    downloadersLimit = Integer.parseInt(getValFromArg(s));
                }
                catch (NumberFormatException ex)
                {
                    downloadersLimit = 0;
                }
            }
            if (s.startsWith("-parsersLimit="))
            {
                try
                {
                    parsersLimit = Integer.parseInt(getValFromArg(s));
                }
                catch (NumberFormatException ex)
                {
                    parsersLimit = 0;
                }
            }
            if (s.startsWith("-maxLevel="))
            {
                try
                {
                    maxLevel = Integer.parseInt(getValFromArg(s));
                }
                catch (NumberFormatException ex)
                {
                    maxLevel = 0;
                }
            }
        }

        return checkArgsAreValid();
    }

    private static String getValFromArg(String arg)
    {
        String[] s = arg.split("=");
        if (s.length == 2)
        {
            return s[1];
        }
        else
        {
            return null;
        }

    }

    private static boolean checkArgsAreValid()
    {
        boolean valid = true;
        try
        {
            File dir = new File(localPath);
            if (!dir.isDirectory())
            {
                valid = false;
            }
        }
        catch (Exception ex)
        {
            valid = false;
        }

        try
        {
            URL url = new URL(site);
        }
        catch (MalformedURLException ex)
        {
            valid = false;
        }

        if (maxBandwith <= 0 || maxCpu <= 0)
        {
            valid = false;
        }

        if (parsersLimit == 0 && downloadersLimit == 0 && maxLevel == 0)
        {
            valid = false;
        }

        return valid;
    }
}
